<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChCtrlRoomScheme;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;

class ChCtrlRoomSchemeMapper extends AbstractMapper
{

    #[Value('cache.default.prefix')]
    protected string $prefix;
    public function assignModel()
    {
        $this->model = ChCtrlRoomScheme::class;
    }

    /**
     * 通过id获取方案
     * @param int $id
     * @return ChCtrlRoomScheme|Model|string|Builder|null
     */
    #[Cacheable('ctrl_room_scheme',value: 'id_#{id}',ttl: 15 * 60)]
    public function findById(int $id): ChCtrlRoomScheme|Model|string|Builder|null
    {
        return $this->model::query()->where('id',$id)->first();
    }

    public function checkExists(int $mainUid,string $roomWxid): bool
    {
        return $this->model::query()
            ->where('main_uid',$mainUid)
            ->where('room_wxid',$roomWxid)
            ->where('status',1)
            ->exists();
    }


    public function delCache(int $id): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'ctrl_room_scheme:id_'.$id;
        $redis->del([$keyOne]);
    }
}